package com.css.view;

import com.css.entity.CustomShortcutKeyKey;
import com.css.entity.CustomShortcutKeyName;
import com.css.entity.KeyMapWrapper;
import com.css.listener.KeyBoarder;
import com.css.listener.MyKeyCode;
import com.css.listener.interfaces.KeyBorderListener;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import lombok.Getter;
import lombok.Setter;

import java.io.IOException;
import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * -
 * </p>
 *
 * @author css
 * @since 2021/4/7
 */
public class CustomShortcutKeyInput extends HBox implements Initializable {
    public TextField shortcutKeyInput;
    public TextField shortcutKeyNameInput;
    private Set<KeyCode> keyCodeSet;
    @Getter
    private List<MyKeyCode> keyCodeList;
    @Getter
    private CustomShortcutKeyName customShortcutKeyName;
    public CustomShortcutKeyInput() {
        System.out.println(2);
    }

    public CustomShortcutKeyInput(CustomShortcutKeyName customShortcutKeyName) {
        this.customShortcutKeyName = customShortcutKeyName;
        this.keyCodeList = new ArrayList<>();
        keyCodeSet = new LinkedHashSet<>();
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getClassLoader().getResource("xml/CustomShortcutKeyInput.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);

        try {
            fxmlLoader.load();
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }

    }

    public void remove(javafx.scene.input.MouseEvent event) {
        Parent parent = getParent();
        if (parent instanceof VBox) {
            ((VBox) parent).getChildren().remove(this);
        }
    }

    public void keyPressed(KeyEvent keyEvent) {
        keyEvent.consume();
        if (!keyEvent.getCode().getName().equals("Undefined")) {
            keyCodeSet.add(keyEvent.getCode());
            keyCodeList = keyCodeSet.stream().map(val -> MyKeyCode.valueOf(keyEvent.getCode().toString())).collect(Collectors.toList());
            StringBuilder sb = new StringBuilder();
            for (KeyCode keyCode : keyCodeSet) {
                sb.append(keyCode.getName());
                sb.append("+");
            }
            sb.setLength(sb.length() - 1);
            shortcutKeyInput.setText(sb.toString());
        }
    }

    public void keyReleased(KeyEvent keyEvent) {
        keyEvent.consume();
        keyCodeSet.remove(keyEvent.getCode());
//        cacheKeyCodeSet.remove(keyEvent.getCode());

        if (!keyCodeSet.isEmpty()) {
        } else {
        }

    }

    KeyBoarder keyBoarder;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        shortcutKeyInput.setPromptText("请按下快捷键");
        shortcutKeyNameInput.setPromptText("请输入快捷键名称");
        if (customShortcutKeyName == null) {
            return;
        }
        shortcutKeyNameInput.setText(customShortcutKeyName.getKeyName());
        if (customShortcutKeyName.getCustomShortcutKeyKeyList() != null) {
            StringBuilder sb = new StringBuilder();
            for (CustomShortcutKeyKey customShortcutKeyKey : customShortcutKeyName.getCustomShortcutKeyKeyList()) {
                keyCodeList.add(customShortcutKeyKey.getMyKeyCode());
                sb.append(customShortcutKeyKey.getMyKeyCode().getName());
                sb.append("+");
            }
            sb.setLength(sb.length() - 1);
            shortcutKeyInput.setText(sb.toString());
        }
//        keyBoarder = new KeyBoarder();
//        keyBoarder.setBorderListener(new KeyBorderListener() {
//            @Override
//            public void onDown(int keyCode) {
//                System.out.println(keyCode);
//                shortcutKeyNameInput.setText(keyCode + "");
//            }
//
//            @Override
//            public void onPress(int keyCode) {
//
//            }
//
//            @Override
//            public void onUp(int keyCode) {
//                KeyMapWrapper.remove(keyCode);
//            }
//        });
    }

    public void setCustomShortcutKeyName(CustomShortcutKeyName customShortcutKeyName) {
        this.customShortcutKeyName = customShortcutKeyName;
    }
}
